{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Deploy PySyft Workers using Docker\n",
    "\n",
    "We will begin by starting our three workers using that docker-compose file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Creating network \"deploy_workers_default\" with the default driver\n",
      "Creating deploy_workers_alice_1 ... \n",
      "Creating deploy_workers_charlie_1 ... \n",
      "Creating deploy_workers_bob_1     ... \n",
      "\u001b[2Bting deploy_workers_charlie_1 ... \u001b[32mdone\u001b[0m\u001b[3A\u001b[2K\u001b[2A\u001b[2K"
     ]
    }
   ],
   "source": [
    "!docker-compose up -d"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then import syft and hook pytorch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:tf_encrypted:Falling back to insecure randomness since the required custom op could not be found for the installed version of TensorFlow. Fix this by compiling custom ops. Missing file was '/usr/lib/python3.7/site-packages/tf_encrypted/operations/secure_random/secure_random_module_tf_1.14.0.so'\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /usr/lib/python3.7/site-packages/tf_encrypted/session.py:26: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /usr/lib/python3.7/site-packages/tf_encrypted/session.py:26: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "import syft\n",
    "from syft import WebsocketClientWorker\n",
    "\n",
    "hook = syft.TorchHook(torch)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here we connect to our three worker using their ids and ports. \n",
    "\n",
    "**Note:** Wait few seconds before running this cell as the deployment may take some time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "alice = WebsocketClientWorker(hook=hook, id=\"alice\", host='127.0.0.1', port=8777)\n",
    "bob = WebsocketClientWorker(hook=hook, id=\"bob\", host='127.0.0.1', port=8778)\n",
    "charlie = WebsocketClientWorker(hook=hook, id=\"charlie\", host='127.0.0.1', port=8779)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can interact with those workers, here we send and get some tensors to make sure that it's working"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([73, 74, 75])\n",
      "tensor([73, 74, 75])\n",
      "tensor([73, 74, 75])\n"
     ]
    }
   ],
   "source": [
    "t = torch.tensor([73, 74, 75])\n",
    "ta = t.send(alice)\n",
    "tb = t.send(bob)\n",
    "tc = t.send(charlie)\n",
    "print(ta.get())\n",
    "print(tb.get())\n",
    "print(tc.get())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here we deployed and interacted with 3 different workers in just some line of code."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Stopping the workers\n",
    "\n",
    "You wouldn't love to have 3 workers running on the background without notice, so don't forget to run this cell to stop all the 3 workers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Stopping deploy_workers_charlie_1 ... \n",
      "Stopping deploy_workers_alice_1   ... \n",
      "Stopping deploy_workers_bob_1     ... \n",
      "\u001b[2Bping deploy_workers_alice_1   ... \u001b[32mdone\u001b[0m\u001b[3A\u001b[2KRemoving deploy_workers_charlie_1 ... \n",
      "Removing deploy_workers_alice_1   ... \n",
      "Removing deploy_workers_bob_1     ... \n",
      "\u001b[2BRemoving network deploy_workers_defaultdone\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "!docker-compose down"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}